package com.topscore.uke.mvp.model;

import com.topscore.uke.entities.TeamStaffData;
import com.topscore.uke.mvp.contractors.GetGroupStaffContractor;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.GroupStaffRequest;
import com.topscore.uke.network.request.PageParams;
import com.topscore.uke.network.retrofit.ApiService;
import com.topscore.uke.network.retrofit.HandleErrorSubscriber;
import com.topscore.uke.network.retrofit.NetClientPostJson;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 71468 on 2017/10/16.
 */

public class GetTeamStaffModel implements GetGroupStaffContractor, GetGroupStaffContractor.Model {

    private final String mGroupCode;

    private GroupStaffRequest request;

    private int mCurrentPage = 1;

    private PageParams mPageParams;

    private ApiService mService;

    public GetTeamStaffModel(String groupCode) {
        this.mGroupCode = groupCode;
        request = new GroupStaffRequest(groupCode);
        mPageParams = new PageParams();
        mPageParams.setPageSize(20);
        mPageParams.setPage(mCurrentPage);
        request.setPageParams(mPageParams);
        mService = NetClientPostJson.getInstance().net().create(ApiService.class);
    }

    @Override
    public void getList(Listener listener) {
        mService.getGroup(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<TeamStaffData>>(listener) {
                    @Override
                    public void onSuccess(BaseResponse<TeamStaffData> response) {
                        listener.onGetListSuccess(response.getData());
                    }
                });

    }

    @Override
    public void getNextPage(Listener listener) {
        int page = mCurrentPage + 1;
        mPageParams.setPage(page);
        mService.getGroup(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<TeamStaffData>>(listener) {
                    @Override
                    public void onSuccess(BaseResponse<TeamStaffData> response) {
                        listener.onGetListSuccess(response.getData());
                    }
                });
    }
}
